﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLVMBAYDomain;

namespace QLVMBAY.Pages
{
    public partial class pKetQuaTim : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session[ClientComm.KEY_TIM_KIEM] != null)
                {
                    var obj = (TTTimKiem)Session[ClientComm.KEY_TIM_KIEM];
                    if (obj.TimCB)
                    {
                        var getLst = ClientComm.Ints.ChuyenBay.GetListCBayByMaTenSB(obj.MaSB, obj.ChieuDi, true);
                        grdChuyenBayDi.DataSource = getLst;
                        grdChuyenBayDi.DataBind();
                        lblTitle.Text = "Kết quả tìm cho điều kiện: <br/> Sân bay " + (obj.ChieuDi ? "đi: " : "đến: ") + obj.TenSB.ToLower();                        
                    }
                    else
                    {

                        var getLst = ClientComm.Ints.ChuyenBay.GetListCBayByDK(obj.DKTim, obj.GiaVe, obj.MaLoaiGhe,
                    obj.ThoiGianDi, true);
                        grdChuyenBayDi.DataSource = getLst;
                        grdChuyenBayDi.DataBind();
                        lblTitle.Text = "";
                        string DK = "";
                        if (obj.GiaVe > 0)
                        {
                            DK= "<br/><li> Giá vé: " + obj.GiaVe;
                        }
                        if (!string.IsNullOrEmpty(obj.MaLoaiGhe))
                        {
                            DK += "<br/><li>Loại ghế: " + obj.TenLoaiGhe; 
                        }
                        if (obj.ThoiGianDi != DateTime.MinValue)
                        {
                            DK += "<br/><li>Ngày bay: " + obj.ThoiGianDi.ToString("dd/MM/yyyy");
                        }
                        lblTitle.Text = "Kết quả tìm cho điều kiện: " + DK;
                    }
                    
                }
            }

        }
        protected void grdChuyenBayDi_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex != -1)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    var ChBay = (ChuyenBay)e.Row.DataItem;
                    var lbl0 = e.Row.FindControl("lblSanBayDi") as Label;
                    lbl0.Text = ChBay.ItemTuyenBay.ItemSanBayDi.TenSanBay;
                    var lbl = e.Row.FindControl("lblSanBayDen") as Label;
                    lbl.Text = ChBay.ItemTuyenBay.ItemSanBayDen.TenSanBay;

                    var lbl1 = e.Row.FindControl("lblTenHang") as Label;
                    lbl1.Text = ChBay.ItemHangHangKhong.TenHang;

                    var lbl11 = e.Row.FindControl("lblNgayBay") as Label;
                    lbl11.Text = ChBay.NgayBay.ToString("dd/MM/yyyy");

                    var lbl2 = e.Row.FindControl("lblTgKhoiHanh") as Label;
                    lbl2.Text = ChBay.TgKhoiHanh.ToString("hh':'mm");

                    var lbl3 = e.Row.FindControl("lblThoiGianBay") as Label;
                    if (ChBay.ThoiGianBay.Days > 0)
                    {
                        lbl3.Text = ChBay.ThoiGianBay.ToString("d'd hh'h'mm'p'");
                    }
                    else if (ChBay.ThoiGianBay.Hours > 0)
                    {
                        lbl3.Text = ChBay.ThoiGianBay.ToString("hh'h'mm'p'");
                    }
                    else
                    {
                        lbl3.Text = ChBay.ThoiGianBay.ToString("mm'p'");
                    }
                }
            }
        }
    }
}